package com.longcom.ui;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import com.longcom.util.HttpClientHelper;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends Activity implements OnClickListener
{
	EditText et_username, et_password;
	Button btn_login;
	String url;
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		url = ((MyApplication)getApplication()).getServiceUrl();
		initView();
	}
	
	private void initView()
	{
		et_username = (EditText) findViewById(R.id.et_username);
		et_password = (EditText) findViewById(R.id.et_password);
		
		et_username.setText("test");
		et_password.setText("123");
		btn_login = (Button) findViewById(R.id.btn_login);
		
		btn_login.setOnClickListener(this);
	}

	@Override
	public void onClick(View view)
	{
		// TODO Auto-generated method stub
		switch (view.getId())
		{
		case R.id.btn_login:
			if(!TextUtils.isEmpty(et_username.getText().toString()) && !TextUtils.isEmpty(et_password.getText().toString()))
			{
				LoginTask task = new LoginTask(et_username.getText().toString(), et_password.getText().toString());
				task.execute();
			}
			
			break;

		default:
			break;
		}
	}
	
	class LoginTask extends AsyncTask<Void, Void, String>
	{
		private String username, password;
		
		public LoginTask(String username, String password)
		{
			this.username = username;
			this.password = password;
		}

		@Override
		protected String doInBackground(Void... params)
		{
			// TODO Auto-generated method stub
			NameValuePair usernamePair = new BasicNameValuePair("username", username);
			NameValuePair passwordPair = new BasicNameValuePair("password", password);
			List<NameValuePair> pairList = new ArrayList<NameValuePair>();
			pairList.add(usernamePair);
			pairList.add(passwordPair);
			String result = HttpClientHelper.postData(url+"LoginServlet", pairList);
			return result;
		}
		
		@Override
		protected void onPostExecute(String result)
		{
			// TODO Auto-generated method stub
			if(result != null)
			{
				
				int id = -1;
				try
				{
					id = Integer.valueOf(result.trim());
				} catch (NumberFormatException e)
				{
					// TODO Auto-generated catch block
					Toast.makeText(getApplicationContext(), "Invalid user", 1).show();
					e.printStackTrace();
					return;
				}
				
				if(-1 != id)
				{
					((MyApplication)getApplication()).setId(id);
					Intent intent = new Intent(LoginActivity.this, MainActivity.class);
					startActivity(intent);
				}
						
			}
			
		}
	}
	

}




















